﻿using MediatR;
using Traceability.App.API.Application.Command.BasicTraceability;
using Traceability.App.Domain.Traceability;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructrue.Interface;

namespace Traceability.App.API.Application.Handler.BasicTraceability
{
    public class GetIdBasicTraceabilityHandler : IRequestHandler<GetIdBasicTraceabilityCommand, APIResult<List<BasicTraceabilityModel>>>
    {
        private readonly IBaseRepository<BasicTraceabilityModel> basicRepo;

        public GetIdBasicTraceabilityHandler(IBaseRepository<BasicTraceabilityModel> basicRepo)
        {
            this.basicRepo = basicRepo;
        }
        public Task<APIResult<List<BasicTraceabilityModel>>> Handle(GetIdBasicTraceabilityCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<BasicTraceabilityModel>> result = new APIResult<List<BasicTraceabilityModel>>();
            var res = basicRepo.GetAll().Where(x => x.Batch == request.Batch && !x.IsDel).ToList();
            if (res == null)
            {
                result.Message = "找不到此编号";
                result.Code = APIEnum.Fail;
                return Task.FromResult(result);
            }
            result.Message = "显示成功";
            result.Code = APIEnum.Success;
            result.Data = res;
            return Task.FromResult(result);
        }
    }
}
